package com.gec.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gec.entity.Jobs;
import com.gec.service.JobsService;
import oracle.jdbc.proxy.annotation.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author GOF
 * @since 2021-11-04
 */
@Controller
@RequestMapping("/jobs")
public class JobsController {

    @Autowired
    private JobsService jobsService;

    @RequestMapping("/list")
    public String showList(Model model,String findName) {
        //包装查询的对象
        QueryWrapper<Jobs> qw = new QueryWrapper<>();
        //设置查询的方式,参数,以及属性对数据库表字段的映射,可使用lambda表达式
        qw.lambda().like(findName!=null,Jobs::getName,findName);
        //将查询方式作为参数传递给查询语句
        List<Jobs> list = jobsService.list(qw);
        model.addAttribute("list", list);
        return "job_list";
    }

    @GetMapping("/add")
    public String add() {
        return "job_add";
    }

    @PostMapping("/add")
    public String add(Jobs jobs) {
        jobsService.save(jobs);
        return "redirect:list";
    }

    @GetMapping("/edit")
    public String edit(Integer id,Model model) {
        Jobs job = jobsService.getById(id);
        model.addAttribute("job",job);
        return "job_edit";
    }

    @PostMapping("/edit")
    public  String edit(Jobs jobs){
        jobsService.saveOrUpdate(jobs);
        return "redirect:list";
    }

    @RequestMapping("/del")
    public String del(Integer id){
        jobsService.removeById(id);
        return "redirect:list";
    }
}
